package com.hsd.znsh.converter;

import com.hsd.znsh.entity.Farm;
import com.hsd.znsh.pojo.output.FarmVO;
import com.hsd.znsh.repository.FarmTypeRepository;
import com.hsd.znsh.repository.UserRepository;
import com.hsd.znsh.utils.BeanUtil;

import java.util.ArrayList;
import java.util.List;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-26 14:48:19
 */
public class FarmConverter {

    public static FarmVO converterToFarmVO(Farm farm,
                                           UserRepository userRepository,
                                           FarmTypeRepository farmTypeRepository){
        FarmVO farmVO=new FarmVO();
        BeanUtil.copyProperties(farm,farmVO);
        if(farm.getCreateUserId()!=null){
            farmVO.setCreateUsername(userRepository.findOne(farm.getCreateUserId()).getUsername());
        }
        if(farm.getFarmTypeId()!=null){
            farmVO.setFarmType(farmTypeRepository.findOne(farm.getFarmTypeId()).getName());
        }
        return  farmVO;
    }

    public static List<FarmVO> converterToFarmVOList(List<Farm> farms,
                                                     UserRepository userRepository,
                                                     FarmTypeRepository farmTypeRepository){
        List<FarmVO> list=new ArrayList<>();
        for(Farm farm:farms){
            list.add(converterToFarmVO(farm,userRepository,farmTypeRepository));
        }
        return  list;
    }
}
